package com.library.controller.user;

import com.library.dto.ReviewDTO;
import com.library.result.Result;
import com.library.service.ReviewService;
import com.library.vo.BookReviewsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/user/review")
@Api(tags = "评论相关接口")
@Slf4j
public class ReviewController {
    @Autowired
    private ReviewService reviewService;

    @PostMapping("/{bookId}")
    @ApiOperation("评论")
    public Result<String> save(@RequestBody ReviewDTO reviewDTO, @PathVariable Long bookId) {
        log.info("评论：{}", reviewDTO);
        reviewService.save(reviewDTO, bookId);
        return Result.success();
    }

    @PostMapping("/reply/{reviewId}")
    @ApiOperation("回复评论")
    public Result<String> reply(@RequestBody ReviewDTO reviewDTO, @PathVariable Long reviewId) {
        log.info("回复评论：{}", reviewDTO);
        reviewService.reply(reviewDTO, reviewId);
        return Result.success();
    }

    @DeleteMapping("/{reviewId}")
    @ApiOperation("删除评论")
    public Result<String> delete(@PathVariable Long reviewId) {
        log.info("删除评论：{}", reviewId);
        reviewService.delete(reviewId);
        return Result.success();
    }

    @GetMapping("/{bookId}")
    @ApiOperation("获取评论列表")
    public Result<List<BookReviewsVO>> list(@PathVariable Long bookId) {
        log.info("获取评论列表：{}", bookId);
        List<BookReviewsVO> list = reviewService.list(bookId);
        return Result.success(list);
    }
}
